<?php

namespace app\forms\group;

use app\forms\Model;
use app\models\Group;
use yii\web\HttpException;
use app\validators\PositiveInteger;

/**
 * 组织编辑时的信息
 */
class InfoForm extends BaseForm
{
    public function rules()
    {
        return [
            ['id', PositiveInteger::class],
            ['id', 'required'],
            ['id', 'exist', 'targetClass' => Group::class],
        ];
    }

    /**
     * @param $param
     * @return array
     * @throws HttpException
     */
    public static function info($param)
    {
        $form = new self();
        $form->setAttributes($param);
        !$form->validate() ? $form->validateError() : '';
        $group = Group::find()->select(['id', 'name', 'parent_id AS parentId', 'sort'])
            ->where(['id' => $form['id']])->asArray()->one();
        $list = (new Group())->getDropList();
        foreach ($list as $i => $arr) {
            if ($arr['value'] == $group['id']) {
                unset($list[$i]);
                break;
            }
        }
        return ['list' => $list, 'group' => $group];
    }
}